package controllers

import (
	"git.oschina.net/fanbuchi/timemaker/models"
	"git.oschina.net/fanbuchi/timemaker/valid"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

// USER API List form、query、path、body、header，form
type UserController struct {
	beego.Controller
}

func init() {

}

func (this *UserController) URLMapping() {
	/*this.Mapping("RegProfile", this.RegProfile)
	this.Mapping("modify", this.ModifyProfile)
	this.Mapping("profile", this.Profile)*/
}

func (this *UserController) Prepare() {

}

// @Author killerzhangsan
// @Title user_list
// @Description app
// @Param   client_id     header    string  true        "The client_id"
// @Success 200 {string} ok
// @router / [get]
func (this *UserController) Get() {
	this.Ctx.WriteString("u api success")
}

// @Author killerzhangsan
// @Title regProfile
// @Description app reg user profile
// @Param   client_id     header    string  true        "The client_id"
// @Param   userName     query    string  true        "The userName for register"
// @Param   nickName     query    string  true        "The nickName for register"
// @Param   password     query    string  true        "The password for register"
// @Success 200 ok
// @Failure 400 Invalid register supplied
// @Failure 404 User not found
// @router /reg [post]
func (this *UserController) RegProfile() {
	userName := this.GetString("userName")
	nickName := this.GetString("nickName")
	pass := this.GetString("password")
	validationResult := valid.ValidProfile(userName, nickName, pass)
	if !validationResult.Ok {
		status := models.ObjectStatus{Status: models.ParamError, Desc: validationResult.Error.Key + " " + validationResult.Error.Message}
		this.Data[dataformat] = status
	} else {
		profile, err := models.CreateProfile(userName, nickName, pass)
		if err != nil {
			status := models.ObjectStatus{Status: models.ParamError, Desc: err.Error()}
			this.Data[dataformat] = status
		} else {
			this.Data[dataformat] = profile
		}

	}

	this.ServeJson()
}

// @Author killerzhangsan
// @Title modifyProfile
// @Description modify profile by userid
// @Param   client_id     header    string  true        "The client_id"
// @Param   basic        header  string  true        "basic token"
// @Param   nick_name     form    string  false        "The nick_name will be modify"
// @Success 200 ok
// @router /modifyProfile [put]
func (this *UserController) ModifyProfile() {
	clientdetails, err := models.Token(this.Ctx.Input.Header("basic"))
	if err != nil {
		status := models.ObjectStatus{Status: models.ParamError, Desc: "aaa" + err.Error()}
		this.Data[dataformat] = status
		this.ServeJson()
		this.StopRun()
	}
	profile := models.Profile{}
	this.ParseForm(&profile)

	if profile.NickName == "" {
		status := models.ObjectStatus{Status: models.ParamError, Desc: "param nick_name is empty"}
		this.Data[dataformat] = status
		this.ServeJson()
		this.StopRun()
	}
	profile.UserId = clientdetails.UserId

	o := orm.NewOrm()
	num, err := o.Update(&profile, "NickName")
	if err != nil || num <= 0 {
		status := models.ObjectStatus{Status: models.ParamError, Desc: err.Error()}
		this.Data[dataformat] = status
		this.ServeJson()
		this.StopRun()
	}

	objectStatus := models.ObjectStatus{Status: 0, Desc: profile.UserName + " success"}
	this.Data[dataformat] = objectStatus
	this.ServeJson()
}

// @Author killerzhangsan
// @Title Profile
// @Description modify  user reg profile
// @Param   client_id     header    string  true        "The client_id"
// @Param   basic        header  string  true        "basic token"
// @Success 200 ok
// @Failure 400 Invalid email supplied
// @Failure 404 User not found
// @router /profile [get]
func (this *UserController) Profile() {
	clientdetails, err := models.Token(this.Ctx.Input.Header("basic"))
	if err != nil {
		status := models.ObjectStatus{Status: models.ParamError, Desc: err.Error()}
		this.Data[dataformat] = status
		this.ServeJson()
		this.StopRun()
	}
	profile := models.GetProfile(clientdetails.UserId)
	this.Data[dataformat] = profile
	this.ServeJson()
}
